const { inf_info, inf_info_type, querySql, op } = require("../../middleware/baseModel");

module.exports = {
  "GET /news/types": async (ctx, next) => {
    const res = await inf_info_type.findAll({ where: { is_usable: 1 }, order: [["sort", "ASC"]] });
    return ctx.success(res);
  },

  "GET /news/detail": async (ctx, next) => {
    let id = ctx.get("id");

    let nextRow = await querySql(`select id,title from  inf_info where id<${id} order by id desc limit 0,1`);
    const res = await inf_info.findOne({ where: { id: id } });
    let upRow = await querySql(`select id,title from  inf_info where id>${id} order by id asc limit 0,1`);

    return ctx.success({
      curRow: res,
      upRow,
      nextRow,
    });
  },

  "POST /news/page": async (ctx, next) => {
    let param = ctx.getPageSize();
    let row = ctx.getBody();
    let { type_id } = row;
    let { key, value } = row.seachOption;
    let where = {
      is_usable: 1,
      info_type_id: type_id,
    };
    if (key && value) {
      where[key] = { [op.like]: "%" + value + "%" };
    }

    const res = await inf_info.findAndCountAll({
      where,
      order: [["id", "DESC"]],
      ...param,
    });
    return ctx.success(res);
  },
};
